package com.wgj.android.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jiaai.xui.view.widget.MultiStatusBut;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.events.PayEvents;
import com.wgj.android.web.WGJWebFragment;
import com.wgj.android.web.entity.WebConfig;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/4.
 */

public class PayDiadlog extends DialogFragment {
    private Dialog dialog ;
    private View view;
    private  String shopName;
    private  int money;

    @BindView(R.id.shopName)
    TextView shopNameText;
    @BindView(R.id.money)
    TextView moneyText;

    @BindView(R.id.weixipay)
    MultiStatusBut weixipayBut;
    @BindView(R.id.alipay)
    MultiStatusBut alipayBut;


    public static PayDiadlog getInstance(String shopName,int money){
        PayDiadlog payDiadlog=new PayDiadlog();
        Bundle bundle=new Bundle();
        bundle.putString("shopName",shopName);
        bundle.putInt("money",money);
        payDiadlog.setArguments(bundle);
        return  payDiadlog;
    }
    @Nullable
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if(dialog==null){
            // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
            initDialogStyle(view);
        }
        return dialog;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shopName=getArguments().getString("shopName");
        money=getArguments().getInt("money");
        if(view==null){
            super.onCreate(savedInstanceState);
            view = LayoutInflater.from(getActivity()).inflate(R.layout.pay_dialog, null, false);
        }
        ButterKnife.bind(this,view);
        shopNameText.setText(shopName);
        moneyText.setText(money+"元");

    }

    private void initDialogStyle(View view) {
        dialog = new Dialog(getActivity(), R.style.dialog_setting);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);
    }


    @OnClick({R.id.weixipay,R.id.alipay}) void onSelectPayType(MultiStatusBut view) {
            switch (view.getId()){
                case R.id.weixipay:
                    view.select(view.isSelected()?false:true);
                    alipayBut.select(false);
                    break;
                case R.id.alipay:
                    view.select(view.isSelected()?false:true);
                    weixipayBut.select(false);
                    break;
            }
    }



    @OnClick({R.id.payBut}) void  onPay(View view){
         XToast.success("充值成功");
         EventBus.getDefault().post(new PayEvents(true,""));
         dismiss();
    }

    @OnClick(R.id.cancel) void  cancel(View view){
         XToast.error("取消充值");
         dismiss();
    }

}
